#Region " Imports "

Imports System.Data
Imports BlazeApps.Library.Links

#End Region

Partial Class Usercontrols_weblinks
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private DataPageLocal As New PagedDataSource

    Protected FTBID As String

    Public Property sortCriteria() As String
        Get
            Return CStr(ViewState("sortCriteria"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    Public Property sortDir() As String
        Get
            Return CStr(ViewState("sortDir"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property

    Protected Property LinkGuid() As Guid
        Get
            Return GetGuidFromVS("LinkID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("LinkID", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetPageTitle("Web Links")

        If IsUserAuthenticated = True Then

            FTBID = GetStringFromQueryString("ftbid")

            If FTBID = String.Empty Then
                Me.uxSubmitButtonPanel.Visible = False

                If Me.Page.IsPostBack = False Then

                    'Set Default Sort Order and Direction
                    Me.sortCriteria = "linktitle"
                    Me.sortDir = "asc"

                    LoadPage()
                End If

                ConfigBaseNavCtrls()

                If Me.Page.IsPostBack = False Then
                    BindDataItems()
                End If

            Else
                Me.uxSaveButtonPanel.Visible = False
                Me.uxGridPanel.Visible = False
            End If

        End If

        Me.uxLinkPanel.Visible = ShowControls(SecurityMask.Editor, True)

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveLink()
        Dim Item As LinksDetail = LinksDetail.NewLinksDetail()

        Item.Linktitle = Me.uxURLNameTextBox.Text.Trim()
        Item.Linkurl = CheckLink(Me.uxLinkTextBox.Text.ToString)
        Item.Userid = CurrentUserID
        Item.CreatedateString = DateTime.Now.ToString()

        Item.Save()

        BindDataItems()

    End Sub

    Private Sub UpdateLink()
        Dim Item As LinksDetail = LinksDetail.GetLinksDetail(LinkGuid)

        Item.Linktitle = Me.uxURLNameTextBox.Text.Trim()
        Item.Linkurl = CheckLink(Me.uxLinkTextBox.Text.ToString)
        Item.Userid = CurrentUserID
        Item.CreatedateString = DateTime.Now.ToString()

        Item.Save()

        BindDataItems()

    End Sub

#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadPageSize(Me.uxPageSizeDropDownList)
    End Sub

    Private Sub GetLink()
        Dim Item As LinksDetail = LinksDetail.GetLinksDetail(LinkGuid)

        Me.uxURLNameTextBox.Text = Item.Linktitle
        Me.uxLinkTextBox.Text = Item.Linkurl
    End Sub

    Public Overloads Overrides Sub BindDataItems()

        Dim List As New LinksList
        Dim DT As New DataTable
        Dim DV As New DataView

        DT = List.GetLinks()

        Me.RecordCount = DT.Rows.Count

        DV.Table = DT

        DV.Sort = Me.sortCriteria & " " & Me.sortDir

        Me.DataPage.DataSource = DV
        Me.DataPage.AllowPaging = True
        Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.DataPage.CurrentPageIndex = Me.PageNumber

        CreatePagingLinks()

        With Me.uxLinksDataList
            .DataSource = Me.DataPage
            .DataBind()
        End With

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
        If Page.IsValid Then
            If LinkGuid = Guid.Empty Then
                SaveLink()
            Else
                UpdateLink()
            End If
        End If
    End Sub

    Protected Sub uxSubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSubmitButton.Click
        SendLinkValue()
    End Sub

    Protected Sub uxResetButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResetButton.Click
        Me.uxLinkTextBox.Text = ""
        Me.uxURLNameTextBox.Text = ""
        LinkGuid = Guid.Empty
    End Sub

    Protected Sub DeleteLink(ByVal sender As Object, ByVal e As CommandEventArgs)
        LinksDetail.DeleteLinksDetail(ConvertToGuid(e.CommandArgument))
        BindDataItems()
    End Sub

    Protected Sub EditLink(ByVal sender As Object, ByVal e As CommandEventArgs)
        LinkGuid = ConvertToGuid(e.CommandArgument)
        GetLink()
    End Sub

    'Protected Sub uxResultsGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles uxResultsGridView.Sorting
    '    'Get the Sort by Value from the Sortexpression field on the column
    '    If Me.sortCriteria = e.SortExpression Then
    '        If Me.sortDir = "desc" Then
    '            Me.sortDir = "asc"
    '        Else
    '            Me.sortDir = "desc"
    '        End If
    '    End If

    '    ' Assign the column clicked to the sortCriteria property
    '    Me.sortCriteria = e.SortExpression

    '    'Calls the function to rebuild the datagrid view, with the new sort order
    '    BindDataItems()
    'End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub SendLinkValue()

        Dim sbScript As New System.Text.StringBuilder

        sbScript.Append(vbCrLf)

        If EnableLinkRedirect() Then
            sbScript.Append("window.opener.FTB_API[""" & FTBID & """].InsertHtml(""<a href='default.aspx?pid=redirect&link=" & CheckLink(Me.uxLinkTextBox.Text.ToString) & "' target='_blank'>" & Me.uxURLNameTextBox.Text.ToString & "</a>"");")
        Else
            sbScript.Append("window.opener.FTB_API[""" & FTBID & """].InsertHtml(""<a href='" & CheckLink(Me.uxLinkTextBox.Text.ToString) & "' target='_blank'>" & Me.uxURLNameTextBox.Text.ToString & "</a>"");")
        End If

        sbScript.Append(vbCrLf)

        sbScript.Append("window.close();")
        sbScript.Append(vbCrLf)

        sbScript.Append(vbCrLf)

        ScriptManager.RegisterStartupScript(uxSubmitButton, Me.GetType(), "SendLinkValues", sbScript.ToString, True)

    End Sub

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

#End Region

End Class
